package edu.hubu.supermarketmanagementsystem.service.impl;

import edu.hubu.supermarketmanagementsystem.entity.ProductFile;
import edu.hubu.supermarketmanagementsystem.mapper.ProductFileMapper;
import edu.hubu.supermarketmanagementsystem.service.ProductFileService;
import edu.hubu.supermarketmanagementsystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductFileServiceImpl implements ProductFileService {

    @Autowired
    private ProductFileMapper productFileMapper;

    @Override
    public ResultVO<String> insertProductFile(ProductFile productFile) {
        productFileMapper.insertProductFile(productFile);
        return ResultVO.success("Product file added successfully");
    }

    @Override
    public ResultVO<ProductFile> getProductFileById(int productRecordId) {
        ProductFile productFile = productFileMapper.selectById(productRecordId);
        if (productFile != null) {
            return ResultVO.success(productFile);
        } else {
            return ResultVO.error(404, "Product file not found");
        }
    }

    @Override
    public ResultVO<List<ProductFile>> getAllProductFiles() {
        List<ProductFile> productFiles = productFileMapper.selectAll();
        return ResultVO.success(productFiles);
    }

    @Override
    public ResultVO<String> updateProductFile(ProductFile productFile) {
        productFileMapper.updateProductFile(productFile);
        return ResultVO.success("Product file updated successfully");
    }

    @Override
    public ResultVO<String> deleteProductFileById(int productRecordId) {
        productFileMapper.deleteProductFileById(productRecordId);
        return ResultVO.success("Product file deleted successfully");
    }

    @Override
    public ResultVO<ProductFile> getProductFileByProductId(int productId) {
        ProductFile productFile = productFileMapper.selectByProductId(productId);
        if (productFile != null) {
            return ResultVO.success(productFile);
        } else {
            return ResultVO.error(404, "Product file not found");
        }
    }

    @Override
    public ResultVO<List<ProductFile>> searchProductFileByName(String productName) {
        List<ProductFile> productFiles = productFileMapper.searchByProductName(productName);
        return ResultVO.success(productFiles);
    }
}
